package com.example.sprinbootdemo.controller;

import com.example.sprinbootdemo.entity.ApiResponse;
import com.example.sprinbootdemo.entity.SalesMan;
import com.example.sprinbootdemo.service.SalesManService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/salesmen")
public class SalesManController {

    private static final Logger logger = LoggerFactory.getLogger(SalesManController.class);

    @Autowired
    private SalesManService salesManService;

    // 获取所有的业务员记录
    @GetMapping("/getList")
    public ResponseEntity<ApiResponse> getList() {
        List<SalesMan> list;
        try {
            list = salesManService.getAllSalesMen();
            ApiResponse response = new ApiResponse(1, "成功", list);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取业务员记录列表失败", e);
            ApiResponse response = new ApiResponse(0, "获取业务员记录列表失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 根据 SalesManID 获取业务员记录
    @GetMapping("/get/{salesManId}")
    public ResponseEntity<ApiResponse> getSalesManBySalesManId(@PathVariable int salesManId) {
        try {
            SalesMan salesMan = salesManService.getAllSalesMen().stream()
                    .filter(s -> s.getSalesManId() == salesManId)
                    .findFirst()
                    .orElse(null);
            if (salesMan == null) {
                ApiResponse response = new ApiResponse(0, "数据不存在", null);
                return ResponseEntity.status(404).body(response);
            }
            ApiResponse response = new ApiResponse(1, "成功", salesMan);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取数据失败, SalesManID: {}", salesManId, e);
            ApiResponse response = new ApiResponse(0, "获取数据失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 添加新的业务员记录
    @PostMapping("/addDate")
    public ResponseEntity<ApiResponse> addSalesMan(@RequestBody SalesMan salesMan) {
        try {
            logger.info("添加业务员记录: {}", salesMan);
            salesManService.addSalesMan(salesMan);
            ApiResponse response = new ApiResponse(1, "业务员记录添加成功", null);
            return ResponseEntity.status(201).body(response); // 返回 201 表示创建成功
        } catch (Exception e) {
            logger.error("添加业务员记录失败", e);
            ApiResponse response = new ApiResponse(0, "添加业务员记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 删除指定 ID 的业务员记录
    @DeleteMapping("/delete/{salesManId}")
    public ResponseEntity<ApiResponse> deleteSalesManBySalesManId(@PathVariable int salesManId) {
        try {
            logger.info("删除业务员记录, SalesManID: {}", salesManId);
            salesManService.deleteSalesManBySalesManId(salesManId);
            ApiResponse response = new ApiResponse(1, "业务员记录删除成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("删除业务员记录失败, SalesManID: {}", salesManId, e);
            ApiResponse response = new ApiResponse(0, "删除业务员记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 更新指定 ID 的业务员记录
    @PutMapping("/update")
    public ResponseEntity<ApiResponse> updateSalesManBySalesManId(@RequestBody SalesMan salesMan) {
        try {
            logger.info("更新业务员记录: {}", salesMan);
            salesManService.updateSalesManBySalesManId(salesMan);
            ApiResponse response = new ApiResponse(1, "业务员记录更新成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("更新业务员记录失败", e);
            ApiResponse response = new ApiResponse(0, "更新业务员记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 根据姓名查询业务员记录
    @GetMapping("/searchByName/{name}")
    public ResponseEntity<ApiResponse> getSalesMenByName(@PathVariable String name) {
        List<SalesMan> salesMen;
        try {
            salesMen = salesManService.getSalesMenByName(name);
            ApiResponse response = new ApiResponse(1, "成功", salesMen);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("根据姓名查询业务员记录失败, Name: {}", name, e);
            ApiResponse response = new ApiResponse(0, "根据姓名查询业务员记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }
}